//global vars
var loading = '<img src="images/loading.gif" alt="Loading..." width="16" height="16" align="absmiddle" />';

function focusInput(arr) {
	if (!arr) {
		return;
	}
	for (var i = 0; i < arr.length; i ++) {	
		var name = arr[i];
		$("input[name='"+name+"']").focus(function(){
			$(this).addClass("inputbox_over");
		}).blur(function(){
			$(this).removeClass("inputbox_over");	
		});
	
	}
}

function initCheckbox(o, name) {
	var cb = $("input[name='"+name+"']");
	if (cb.length) {
		if (cb.val() == "true") {
			cb.val("false");
			$(o).removeClass("checkbox_checked");
		} else {
			cb.val("true");
			$(o).addClass("checkbox_checked");
		}
	}
}


function hash() {
	if (arguments.length) {
		window.location.hash = arguments[0];
	}
	return window.location.hash.substr(1);
}

function getRect() {
	return {
		viewHeight : self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
		viewWidth : self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
		scrollTop : self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop,
		scrollLeft : self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft
	};
}

function string2Array(str) {
	var out = [];
	if (typeof str !== "string") {
		str = str.toString();
	}
	if (str.length) {
		var arr = str.split(/\s*\,\s*/);
		for (var i = 0; i < arr.length; i ++) {
			if (arr[i] != "") {
				out.push(arr[i]);
			}
		}
	}
	return out;
}

function string2object(str) {
	var obj = {};
	if (typeof str !== "string") {
		str = str.toString();
	}
	if (str.length) {
		str = str.replace(/[\{\}]/g, "");
		str = str.replace(/\[.*?\]/g, function(){	
			return escape(arguments[0]);
		});
		var arr = str.split(/\s*\,\s*/g);
		for (var i = 0; i < arr.length; i ++) {
			var s = arr[i];
			var a = s.split(/\s*\:\s*/g);
			if (a.length == 2) {
				obj[a[0]] = unescape(a[1]);	
			}
		}
	}
	return obj;
}


function strlen(str) {
	var len = 0;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? 3 : 1;
	}
	return len;
}

function cutstr(str, maxlen, dot) {
	var len = 0;
	var ret = '';
	var dot = !dot ? '...' : '';
	maxlen = maxlen - dot.length;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? 3 : 1;
		if(len > maxlen) {
			ret += dot;
			break;
		}
		ret += str.substr(i, 1);
	}
	return ret;
}

function preg_replace(search, replace, str, regswitch) {
	var regswitch = !regswitch ? 'ig' : regswitch;
	var len = search.length;
	for(var i = 0; i < len; i++) {
		re = new RegExp(search[i], regswitch);
		str = str.replace(re, typeof replace == 'string' ? replace : (replace[i] ? replace[i] : replace[0]));
	}
	return str;
}
function htmlspecialchars(str) {
	return preg_replace(['&', '<', '>', '"'], ['&amp;', '&lt;', '&gt;', '&quot;'], str);
}

function formatTime(t){
	//2006-12-12 12:12:12
	var t1=t.split(" ");
	var t11=t1[0].split("-");
	var t12=t1[1].split(":");
	return new Date(t11[0],t11[1],t11[2],t12[0],t12[1],t12[2]);
}

function highlight(o,c){
	o.style.backgroundColor=c;
	o.onmouseout=function(){
		o.style.backgroundColor="";
	}
}
function checkAll(o, form){
	for (var i=0;i<form.elements.length;i++){
		var e = form.elements[i];
		if (e.type=="checkbox" && e.name.indexOf("idlist")!=-1 && e.disabled==false){
			e.checked = o.checked;
		}
	}
}

//functions ========================================================================


function parseJSON(data) {
	return (new Function("return " + data))();
}

function trim(text) {
	return text == null ? "" : text.toString().replace( /^\s+/, "" ).replace( /\s+$/, "" );
}

function loadJS(htm, src) {
	var iframe = document.createElement("iframe");
	iframe.frameBorder = iframe.width = iframe.height = 0;
	document.body.appendChild(iframe);
	var iframeWin = iframe.contentWindow || iframe.contentDocument;
	var iframeDoc = iframeWin.document;
	iframeDoc.write('<!doctype><html><body>'+htm+'<'+'/body><'+'/html>');
	var script = iframeDoc.createElement("script");
	script.type = "text/javascript";
	script.src = src;
	script.onload = script.onreadystatechange = function() {
		iframeDoc.close();
	};
	iframeDoc.body.appendChild(script);
}
function parseXML(data) {
	var xml, tmp;
	if (window.DOMParser) {
		tmp = new DOMParser();
		xml = tmp.parseFromString(data , "text/xml");
	} else {
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async = "false";
		xml.loadXML(data);
	}
	tmp = xml.documentElement;
	if (!tmp || !tmp.nodeName || tmp.nodeName === "parsererror" ) {
		alert("Invalid XML: " + data);
	}
	return xml;
}

Math.clamp = function(num, min, max) { 
	return Math.max(Math.min(num, max), min); 
}

Array.prototype.filter = function() {
	var l = this.length;
	if (l) {
		var obj = {};
		for (var i = 0; i < l; i ++) {
			obj[this[i]] = this[i];	
		}
		this.length = 0;
		for (var k in obj) {
			this.push(k);	
		}
	}
};


//coolie//////////////////////////////////////////
function cookie_set(name, value, expires) {
	var str = name + "=" + encodeURIComponent(value);
	if (isNaN(expires)) {
		expires = 3600;
	}
	var date = new Date();
	date.setTime(date.getTime() + expires * 1000);
	str += "; expires=" + date.toGMTString();
	document.cookie = str;
}
function cookie_get(name) {
	var str = document.cookie;
	var cookieName = name + "=";
	var valueBegin = str.indexOf(cookieName);
	if (valueBegin == -1) {
		return "";
	}
	var valueEnd = str.indexOf(";", valueBegin);
	if (valueEnd == -1) {
		valueEnd = str.length;
	}
	var value = str.substring(valueBegin + cookieName.length, valueEnd);
	if (value) {
		value = decodeURIComponent(value);	
	}
	return value;
}
function cookie_del(name) {
	cookie_set(name, "", -1);
}